// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import VueRouter from 'vue-router'
import routes from './routes'
import store from './store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import Vuex from 'vuex'
import axios from 'axios'
import 'font-awesome/css/font-awesome.min.css'
import './assets/ifont/iconfont.css'
import './assets/ifont/iconfont.ttf'
import './assets/ifont/iconfont.woff'
import './assets/ifont/iconfont.woff2'
import Chat from 'jwchat'

Vue.config.productionTip = false
Vue.prototype.$axios = axios
Vue.use(ElementUI)
Vue.use(Chat)
Vue.use(VueRouter)
Vue.use(Vuex)
// 获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
// 修改原型对象中的push方法
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // 如果访问的是登录页面，直接放行
  if (to.path === '/login') {
    next()
  } else {
    // 如果访问的是路由器中的其他组件，判断是否登录
    if (store.getters.isLogin === false) {
      var user = JSON.parse(sessionStorage.getItem('user'))
      if (user != null) {
        store.commit('saveUser', user)
      }
    }
    if (store.getters.isLogin) {
      next()
    } else {
      next('/')
    }
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
